package com.course.cases;

import com.alibaba.fastjson.JSONObject;
import com.course.config.TestConfig;
import com.course.model.InterfaceName;
import com.course.model.Login;
import com.course.utils.ConfigFile;
import com.course.utils.DatabaseUtil;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.cookie.Cookie;
import org.apache.http.entity.StringEntity;
import org.apache.http.util.EntityUtils;
import org.apache.ibatis.session.SqlSession;
import org.testng.Assert;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

import java.io.IOException;
import java.util.List;

/**
 * @author bigFish
 * @Title: public:
 */
public class LoginTest {


    @BeforeTest(groups = "loginTrue", description = "测试前准备工作，获取httpClient对象")
    public void beforeTest() {
        //为配置文件中变量赋值
        TestConfig.getUserInfoUrl = ConfigFile.getUrl(InterfaceName.GETINFO);
        TestConfig.getUserListUrl = ConfigFile.getUrl(InterfaceName.GETUSERLIST);
        TestConfig.upUserUrl = ConfigFile.getUrl(InterfaceName.UPUSER);
        TestConfig.loginUrl = ConfigFile.getUrl(InterfaceName.LOGIN);
        TestConfig.addUserUrl = ConfigFile.getUrl(InterfaceName.ADDUSER);

        TestConfig.closeableHttpClient = TestConfig.getHttpClient();


    }

    @Test(groups = "loginTrue",description = "用户成功登陆接口")
    public void loginTrue() throws IOException {

        SqlSession sqlSession = DatabaseUtil.getSqlSession();
        Login login = sqlSession.selectOne("login", 1);
        System.out.println(login.toString());
        System.out.println(TestConfig.loginUrl);

        //下边的代码为写完接口的测试代码
        String result = getResult(login);
        //处理结果，
        List<Cookie> cookies = TestConfig.cookieStore.getCookies();
        System.out.println(cookies);
        Assert.assertEquals(login.getExpected(),result);


    }

    @Test(description = "用户登陆失败接口")
    public void loginFalse() throws IOException {
        SqlSession session = DatabaseUtil.getSqlSession();

        Login login = session.selectOne("login",2);
        System.out.println(login.toString());
        System.out.println(TestConfig.loginUrl);

        //下边的代码为写完接口的测试代码
        String result = getResult(login);
        //处理结果，就是判断返回结果是否符合预期
        Assert.assertEquals(login.getExpected(),result);

    }



    private String getResult(Login login) throws IOException {
        //下边的代码为写完接口的测试代码
        HttpPost post = new HttpPost(TestConfig.loginUrl);
        JSONObject param = new JSONObject();
        param.put("userName",login.getUserName());
        param.put("password",login.getPassword());
        //设置请求头信息 设置header
        post.setHeader("content-type","application/json");
        //将参数信息添加到方法中
        StringEntity entity = new StringEntity(param.toString(),"utf-8");
        post.setEntity(entity);
        //声明一个对象来进行响应结果的存储
        String result;
        //执行post方法

        HttpResponse response = TestConfig.closeableHttpClient.execute(post);
        //获取响应结果
        System.out.println(response);
        result = EntityUtils.toString(response.getEntity(),"utf-8");
        System.out.println(result);
//        TestConfig.cookieStore = TestConfig.ookieStore;
        TestConfig.token = response.getFirstHeader("token");
        return result;
    }
}
